#ifndef __BOARD_BASE_H
#define __BOARD_BASE_H
#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                               Includes                                |
-----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "iSwitch.h"
#include "main.h"
#include "gpio.h"
#include "usart.h"
#include "tim.h"
//#include "FreeRTOS.h"
//#include "queue.h"
//#include "task.h"
//#include "timers.h"
#include "stimer.h"

/*-----------------------------------------------------------------------
|                                define                                 |
-----------------------------------------------------------------------*/
//*** <<< Use Configuration Wizard in Context Menu >>> ***
//<h> setting

//</h>

//*** <<< end of configuration section >>>    ***
/* fast pin operate */
// Pin input read
#define Pinx_Read(Labelx)	(!!((Labelx##_GPIO_Port->IDR & Labelx##_Pin)))
// Pin output read
#define Pinx_ORead(Labelx)	(!!((Labelx##_GPIO_Port->ODR & Labelx##_Pin)))
// Pin set high
#define Pinx_SET(Labelx)	(Labelx##_GPIO_Port->BSRR = (uint32_t)Labelx##_Pin)
// Pin set low
#define Pinx_RESET(Labelx)	(Labelx##_GPIO_Port->BRR = (uint32_t)Labelx##_Pin)
// Pin write out
#define Pinx_Write(Labelx, state) (((state) != 0) ? Pinx_SET(Labelx) : Pinx_RESET(Labelx))
// Pin togger out
#define Pinx_Togger(Labelx)                                                                   \
        do{                                                                                   \
            uint32_t odr = (Labelx##_GPIO_Port->ODR & Labelx##_Pin);                          \
            Labelx##_GPIO_Port->BSRR = ((odr & Labelx##_Pin) << 16) | (~odr & Labelx##_Pin);  \
        } while (0)

/* board LED ctrl */
// Buzzer
#define Buzzer_write(x) Pinx_Write(Buzzer, !x)
#define Buzzer_read()   Pinx_ORead(Buzzer)
#define Buzzer_on()     Pinx_RESET(Buzzer)
#define Buzzer_off()    Pinx_SET(Buzzer)
// LEDx Control
#if 0 // 0:Low voltage is valid
#define LEDx_Write(LEDx, state) Pinx_Write(LEDx, (state))
#define LEDx_Read(LEDx) (!!Pinx_ORead(LEDx))
#else //
#define LEDx_Write(LEDx, state) Pinx_Write(LEDx, !(state))
#define LEDx_Read(LEDx) (!Pinx_ORead(LEDx))
#endif
#define LEDx_Togger(LEDx) Pinx_Togger(LEDx)
// board KEY Read input state
#define KEYx_Read(KEYx, index) ((Pinx_Read(KEYx)) << index)

		
#define LED_RESET	 (LED_GPIO_Port->BRR |= LED_Pin)
#define LED_SET 	 (LED_GPIO_Port->BSRR |= LED_Pin)
#define RGBSET(R_,G_,B_)   (R_<<16 | G_ << 8 | B_)
#define RGBREAD(vRGB) vRGB>>16,vRGB>>8,vRGB
typedef enum LEDStateStructure{
    LED_RUN = RGBSET(0,0x10,0),
    LED_NOPOWER_LOW = RGBSET(0xFF,0,0),
    LED_OFF = RGBSET(0,0,0),
    LED_MAX = 0xFFFFFF
}LEDState_e;
/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/
#define  BSP_KEY_NUM 3
extern iSW_t hisw1[BSP_KEY_NUM];
extern LEDState_e LED_State;
void bsp_MesureTimeStart(uint32_t *timecnt);
uint32_t bsp_MesureTime(uint32_t *timecnt);
void board_base_init(void);
void bsp_key_init(void);
void LED_Run(const void *arg);
void RGB_SET(uint8_t LED,uint8_t R, uint8_t G, uint8_t B);
extern uint16_t TaskBuzzerID;
void TaskBuzzer(const void *arg);
void Beep(uint32_t time);
void bsp_key_input_raed(uint8_t *results, uint16_t num);
#ifdef __cplusplus
}
#endif

#endif
